package com.sina.oefund;

/**
 * Created by IntelliJ IDEA.
 * User: qtang
 * Date: Jul 4, 2013
 * Time: 1:57:59 PM
 * To change this template use File | Settings | File Templates.
 */
public class SinaOEFund {
    private String code;
    private String name;
    private double price;
    private String priceDate;

    public static class ParseStockInfoException extends Exception{
        public ParseStockInfoException(){
            super("Parse StockInfo error!");
        }
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getPriceDate() {
        return priceDate;
    }

    public void setPriceDate(String priceDate) {
        this.priceDate = priceDate;
    }

    public SinaOEFund(String code, String name, double price, String priceDate) {
        this.code = code;
        this.name = name;
        this.price = price;
        this.priceDate = priceDate;
    }

    public static SinaOEFund parseFund(String source) throws ParseStockInfoException {
        int beginIndex = source.indexOf("\"",0);
        int endIndex = source.indexOf("\"",beginIndex+1);
        String subSource =  source.substring(beginIndex,endIndex);
        String[] dataArray = subSource.split("~");
        SinaOEFund fund = null;
        if(dataArray.length == 9){
            fund = new SinaOEFund(dataArray[0], dataArray[1], Double.parseDouble(dataArray[5]), dataArray[8]);
        }
        return fund;
    }

    @Override
    public String toString() {
        return "SinaOEFund{" +
                "code='" + code + '\'' +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", priceDate='" + priceDate + '\'' +
                '}';
    }
}
